package com.vcommon.core.managers.utils
{
import com.greensock.TimelineLite;
import com.greensock.TweenLite;
import com.greensock.easing.Back;
import com.greensock.easing.Circ;
import com.greensock.plugins.AutoAlphaPlugin;
import com.greensock.plugins.ShortRotationPlugin;
import com.greensock.plugins.TransformMatrixPlugin;
import com.greensock.plugins.TweenPlugin;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;

/**
 * 弹出层效果
 * author yangwz
 */		
public class TweenUtil
{
	/**
	 * 保存弹出窗口时的效果，需要在动画结束后disopose 
	 */	
	static private var popUpObject:Object = {};
	
	/**
	 * 保存关闭窗口是时的效果，需要在动画结束后dispose
	 */	
	static private var removeObject:Object = {};
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>TweenUtil</code> instance.
	 * 
	 */
	public function TweenUtil()
	{
	}
	TweenPlugin.activate([AutoAlphaPlugin]);
	TweenPlugin.activate([ShortRotationPlugin, 
		TransformMatrixPlugin]);
	
	/**
	 * 弹出动画 
	 * @param container 父对象
	 * @param target 当前对象，需要显示的对象
	 * 
	 */	
	static public function popUp(container:DisplayObjectContainer, target:DisplayObject):void
	{
		target.alpha = 0.8;
		target.scaleX = target.scaleY = 0.9;
		
		container.addChild(target);
		var timeline:TimelineLite = new TimelineLite({onComplete:popUpCompleteHandler,
			onCompleteParams:[container, target]});
		popUpObject[target] = timeline;
		timeline.append(new TweenLite(target, 0.6, {transformMatrix:{scaleX:1, scaleY:1},/*scaleX:1, scaleY:1,*/ ease:Back.easeOut}));
		timeline.insert(new TweenLite(target, 0.4, {autoAlpha:1, ease:Circ.easeInOut}), 0.1);
	}
	
	static public function remove(container:DisplayObjectContainer, target:DisplayObject):void
	{
		var timeline:TimelineLite = new TimelineLite({autoRemoveChildren:true,
			onComplete:removeCompleteHandler,
			onCompleteParams:[container, target]});
		removeObject[target] = timeline;
		
		timeline.append(new TweenLite(target, 0.5, {transformMatrix:{scaleX:0.7, scaleY:0.7}, ease:Back.easeIn}));
		timeline.insert(new TweenLite(target, 0.3, {autoAlpha:0, ease:Circ.easeIn}), 0.2);
	}
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	static private function popUpCompleteHandler(container:DisplayObjectContainer, target:DisplayObject):void
	{
		if (popUpObject[target])
		{
			target.x = target.x >> 0;
			target.y = target.y >> 0;
			popUpObject[target].clear();
			popUpObject[target] = null;
			delete popUpObject[target];
		}
	}
	
	static private function removeCompleteHandler(container:DisplayObjectContainer, target:DisplayObject):void
	{
		if (removeObject[target])
		{
			removeObject[target].clear();
			removeObject[target] = null;
			delete removeObject[target];
		}
		if (target.parent && target.parent.contains(target))
		{
			target.parent.removeChild(target);
		}
	}
}
}